package com.horay.statemachiedemo.multi.persist;

import com.horay.statemachiedemo.multi.constant.StateMachineConstant;
import com.horay.statemachiedemo.multi.enums.OrderState;
import com.horay.statemachiedemo.multi.event.OrderEvent;
import com.horay.statemachiedemo.multi.model.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.statemachine.StateMachineContext;
import org.springframework.statemachine.StateMachinePersist;
import org.springframework.statemachine.support.DefaultStateMachineContext;
import org.springframework.stereotype.Repository;

/**
 * Class description
 *
 * @author heyingcheng
 * @date 2020/10/23
 */
@Slf4j
@Repository("orderDbStateMachinePersist")
public class OrderDbStateMachinePersist implements StateMachinePersist<OrderState, OrderEvent, Order> {

    @Override
    public void write(StateMachineContext<OrderState, OrderEvent> context, Order order) throws Exception {
        //这里不做任何持久化工作
    }

    @Override
    public StateMachineContext<OrderState, OrderEvent> read(Order order) throws Exception {
        StateMachineContext<OrderState, OrderEvent> result = new DefaultStateMachineContext<>(OrderState.valueOf(order.getOrderState()), null, null, null, null, StateMachineConstant.ORDER_MACHINE_ID);
        return result;
    }

}
